<?php

/*
	info: 网站首页
	date: 2011-12-12
*/

define('PAGESCRIPT', 'index');
require dirname(__FILE__).'/include/common.inc.php';
$webglobal['file_index'] = array();

//获取站点公告

$newsCategoryDataModel = CategoryDataModel::factory('news', array('cache' => true));

//cache_GetNotice($newsCategoryDataModel); //$webcache['notice']

//获取首页显示的新闻
//cache_GetShowIndexNews($newsCategoryDataModel); //$webcache['showindex']

//获取热点信息
//cache_GetHotNews($newsCategoryDataModel); //$webcache['hotnews']

//获取最新优惠劵
cache_GetNewCoupon(); //$webcache['newcoupon']

//获取热门优惠劵
cache_GetHotCoupon(); //$webcache['hotcoupon'](按日,周,月分组)
//获取最新商户
cache_GetNewCompany(); //$webcache['newcompany']

//获取商圈信息
//获取城市商圈
$webglobal['file_index']['areadata'] = array();
if (isset($webglobal['curcityid']) && $webglobal['curcityid']) {
	if ($areadata = $areaDataModel->getChildForLayer($webglobal['curcityid'], 2, false)) {
		foreach ($areadata as $key => $value) {
			if (isset($webglobal['file_index']['areadata'][$value['upid']])) {
				$webglobal['file_index']['areadata'][$value['upid']]['second'][$value['id']] = $value;
			} else {
				$webglobal['file_index']['areadata'][$value['id']] = $value;
				$webglobal['file_index']['areadata'][$value['id']]['second'] = array();
			}
		}
	}
}

//获取优惠劵分类
$webglobal['file_index']['couponCategory'] = array();
foreach ($webglobal['couponCategory'] as $couponid => $coupon) {
	if (isset($webglobal['file_index']['couponCategory'][$coupon['upid']])) {
		$webglobal['file_index']['couponCategory'][$coupon['upid']]['cell'][$couponid] = array(
			'name' => $coupon['name']
		);
	} else {
		$webglobal['file_index']['couponCategory'][$couponid] = array(
			'name' => $coupon['name'],
			'cell' => array()
		);
	}
}

$companyCommentFiles = array('`tm_user`.`nickname` as personname', '`tm_user`.`avatar` as avatar',
							'`tm_company`.`name` as companyname',
							'`tm_company_comment`.id','`tm_company_comment`.`photo`',
							'`tm_company_comment`.title','`tm_company_comment`.`content`',
							'`tm_company_comment`.inquiry_title','`tm_company_comment`.inquiry_result',
							'`tm_company_comment`.`floor`','`tm_company_comment`.ref_id',
							'`tm_company_comment`.`score`','`tm_company_comment`.`addtime`',
							'`tm_company_comment`.`status`','`tm_company_comment`.user_type',
							'`tm_company`.`id` as companyid'
);

$webglobal['file_companyComment'] = array();
$fileSql = implode($companyCommentFiles,',');
$LIMIT =2;
$query = $db->Query("SELECT {$fileSql} FROM `tm_company_comment` LEFT JOIN `tm_company` ON 
`tm_company_comment`.`company_id` = `tm_company`.`id` LEFT JOIN `tm_user` ON 
`tm_company_comment`.`user_id` = `tm_user`.`id` WHERE `tm_company_comment`.`status` = '1' AND `tm_company`.`status` = 1 ORDER BY `tm_company_comment`.`id` DESC LIMIT {$LIMIT}");

while ($tempdata = $db->FetchArray($query)) {
	$tempdata['score'] = ($tempdata['score'] >=0 && $tempdata['score'] <= 5) ? intval($tempdata['score']) : 5;
	$tempdata['addtime'] =  !empty($tempdata['addtime']) ? global_FormatTime($tempdata['addtime']) : '';
	$webglobal['file_companyComment'][$tempdata['id']] = $tempdata;
}

//获取明星商铺
$webglobal['file_index']['starCompany'] = array();
if ($webglobal['file_index']['starCompany'] = $db->GetOne("SELECT * FROM {$webconfig['dbpre']}company WHERE city = {$webglobal['curcityid']} AND newstar = 1")) {
	if ($webglobal['file_index']['starCompany']['logo'] != '') {
		$webglobal['file_index']['starCompany']['star'] = getThumbnails($webglobal['file_index']['starCompany']['logo'], 'star');
	}
	if ($webglobal['file_index']['starCompany']['image'] != '') {
		$webglobal['file_index']['starCompany']['thumbnail'] = array(
			'big' => getThumbnails($webglobal['file_index']['starCompany']['image'], 'big'),
			'small' => getThumbnails($webglobal['file_index']['starCompany']['image'], 'small'),
			'middle' => getThumbnails($webglobal['file_index']['starCompany']['image'], 'middle')
		);
	}
}

//获取公告
$notic = array(
	'row' => 5,
	'titlelen' => 30,
	'typeid' => 1
);

$webglobal['file_index']['notice'] = getArcList($notic);

//获取图片信息
$image = array(
	'row' => 5,
	'titlelen' => 30,
	'typeid' => 3,
	'listtype'=> 'image.commend.'
);

$webglobal['file_index']['image'] = getArcList($image);
//获取热点信息
$hotnews = array(
	'row' => 8,
	'titlelen' => 30,
	'typeid' => 2,
	'listtype'=> 'commend.'
);
$webglobal['file_index']['hotnews'] = getArcList($hotnews);

//获取分类信息图片导航
$typeimage = array(
	'limit' => 1,
	'titlelen' => 30,
	'infolen' => 120,
	'typeid' => 2,
	'listtype'=> 'image.commend.'
);
$webglobal['file_index']['typeimage'] = array();
$webglobal['file_index']['typeimage'] = getArcList($typeimage);

//获取分类信息
$webglobal['file_index']['channelartlist'] = array();
if ($rootTypes = $newsCategoryDataModel->getChildForLayer(2, 1, false)) {
	foreach ($rootTypes as $type) {
		$channelList = array(
			'titlelen' => '60',
			'row'   => 2,
			'typeid' => $type['id']
		);
		$type['typeurl'] = WEBURLPATH . '/news.php?action=type&id=' . $type['id'];
		$type['typename'] = $type['name'];
		$type['list'] = array();
		$type['list'] = getArcList($channelList);
		$webglobal['file_index']['channelartlist'][] = $type;
	}
}

template_Display('index');
